/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.unipampa.dados;

import br.unipampa.view.ApoioDeInterface;
import com.mysql.jdbc.Statement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 * @author Gabriel Moro
 * @since 15/03/2013
 * @version 1.2
 */
public class DBConfigCalendar extends DML {

    private ArrayList<Integer> values;

    public DBConfigCalendar(Connection con) {
        super.connection = (com.mysql.jdbc.Connection) con;
        super.table = "configdecalendar";
    }

    public ArrayList<Integer> selected(String mes, String ano) {
        try {
            int codAno = validationCodigoAno(ano);
            int codMes = validationCodigoMes(mes);

            super.statement = (Statement) super.connection.createStatement();
            super.resultSet = super.statement.executeQuery("SELECT positionInicial_configDeCalendar,valueTerm_configDeCalendar FROM " + super.table + " WHERE codAno_configDeCalendar =" + codAno
                    + " and codMes_configDeCalendar=" + codMes + ";");
            this.values = new ArrayList<>();
            while (super.resultSet.next()) {
                this.values.add(super.resultSet.getInt(1));//position
                this.values.add(super.resultSet.getInt(2));//valor que termina
            }
            return values;
        } catch (SQLException erroSQl) {
            ApoioDeInterface.mensagemErroBanco(erroSQl.getMessage());
        }
        return null;
    }

    @Override
    public ArrayList<Object> select(String coluna) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public int insert(String valores) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public int update(String atributoX, String valores) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public int delete(String valor) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void closedConnection() {
        try {
            super.connection.close();
            super.statement.close();
            super.resultSet.close();
        } catch (SQLException erroSql) {
            ApoioDeInterface.mensagemErroBanco(erroSql.getMessage());
        }
    }

    private int validationCodigoMes(String mes) {
        try {
            java.sql.Statement stm = (com.mysql.jdbc.Statement) super.connection.createStatement();
            ResultSet rs = stm.executeQuery("SELECT codigo_meses" + " FROM meses WHERE nome_meses= '" + mes + "';");
            int value = 0;
            while (rs.next()) {
                value = rs.getInt(1);
            }
            stm.close();
            rs.close();
            return value;
        } catch (SQLException ex) {
            ApoioDeInterface.mensagemErroBanco(ex.getMessage());
        }
        return 0;
    }

    private int validationCodigoAno(String ano) {
        try {
            java.sql.Statement stm = (com.mysql.jdbc.Statement) super.connection.createStatement();
            ResultSet rs = stm.executeQuery("SELECT codigo_anos" + " FROM anos WHERE nome_anos= '" + ano + "';");
            int value = 0;
            while (rs.next()) {
                value = rs.getInt(1);
            }
            stm.close();
            rs.close();
            return value;
        } catch (SQLException ex) {
            ApoioDeInterface.mensagemErroBanco(ex.getMessage());
        }
        return 0;
    }
}
